package com.danger.Activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.util.Attributes;
import com.danger.Adapter.SwipeListViewAdapter;
import com.danger.Model.ListViewBean;

import java.util.ArrayList;
import java.util.List;

public class SwipeListViewActivity extends AppCompatActivity {

    private ListView listView;
    private SwipeListViewAdapter adapter;
    private List<ListViewBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swipe_list_view);

        initView();
        initData();
        initEvent();

    }

    private void initView() {
        listView = (ListView) findViewById(R.id.listView);
    }

    private void initData() {
        list = new ArrayList<>();
        ListViewBean bean1 = new ListViewBean("服务号", "15:28", "QQ天气：[镇雄] 阵雨 14° /21°", R.mipmap.test1);
        ListViewBean bean2 = new ListViewBean("新朋友", "昨天", "邓彦 请求添加好友", R.mipmap.test2);
        ListViewBean bean3 = new ListViewBean("群助手", "", "群助手可查看“收进群助手且不提醒”的消息", R.mipmap.test3);
        ListViewBean bean4 = new ListViewBean("我最亲爱的", "16:50", "再见！！！", R.mipmap.test4);
        list.add(bean1);
        list.add(bean2);
        list.add(bean3);
        list.add(bean4);
        adapter = new SwipeListViewAdapter(this, list);
        listView.setAdapter(adapter);
        adapter.setMode(Attributes.Mode.Single);
    }

    private void initEvent() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ((SwipeLayout) (listView.getChildAt(position - listView.getFirstVisiblePosition()))).open(true);
            }
        });
        listView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.e("ListView", "OnTouch");
                return false;
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(SwipeListViewActivity.this, "OnItemLongClickListener", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                Log.e("ListView", "onScrollStateChanged");
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });

        listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Log.e("ListView", "onItemSelected:" + position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Log.e("ListView", "onNothingSelected:");
            }
        });
    }
}
